﻿using DevExpress.Utils;
using MDIDemo.Model;
using MDIDemo.PublicClass;
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DevMy.Vou
{
    public partial class Form_Project : Form_BaseAlone
    {

        public Form_Project(string FormText)
        {
            InitializeComponent();
            SetComponent(FormText);
        }
        private WaitDialogForm waitDialogForm;
        private Class_MySqlDataBase class_MySqlDataBase;
        private void SetComponent(string FormText)
        {
            base.SetComponent(FormText, "I0011");
            base.SetGridDataSource(null);
            base.GetGridPage().PageSize = 50;
            base.EnableViewButton(false);
            base.EnableOutButton(false);

            Class_PublicMethod class_PublicMethod = new Class_PublicMethod();
            Class_DataBaseConDefault class_DataBaseConDefault = new Class_DataBaseConDefault();
            class_DataBaseConDefault = class_PublicMethod.FromXmlToDefaultValueObject<Class_DataBaseConDefault>("DataBaseDefaultValues");

            class_MySqlDataBase = new Class_MySqlDataBase(class_DataBaseConDefault.dataSourceUrl
                , class_DataBaseConDefault.dataBaseName
                , class_DataBaseConDefault.dataSourceUserName
                , class_DataBaseConDefault.dataSourcePassWord
                , class_DataBaseConDefault.Port);
        }
        public override int AddData()
        {
            int index = -1;
            Form_ProjectBase form_ProjectBase = new Form_ProjectBase();
            form_ProjectBase.SetWinType(1);
            form_ProjectBase.id = null;
            form_ProjectBase.Ini();
            form_ProjectBase.Text = this.Text;
            if (form_ProjectBase.ShowDialog() == DialogResult.OK)
            {
                string Sql = string.Format(@"Insert Into vou_project(id,projectName
                    ,projectAddress
                    ,projectManager
                    ,projectType
                    ,remark)
                    Values ('{0}','{1}','{2}','{3}','{4}','{5}')"
                    , form_ProjectBase.projectMainModel.id
                    , form_ProjectBase.projectMainModel.projectName
                    , form_ProjectBase.projectMainModel.projectAddress
                    , form_ProjectBase.projectMainModel.projectManager
                    , form_ProjectBase.projectMainModel.projectType
                    , form_ProjectBase.projectMainModel.remark);
                index = class_MySqlDataBase.ExecuteSql(Sql);
            }
            form_ProjectBase.Dispose();
            return index;
        }
        public override int EditData(object id)
        {
            int index = -1;
            Form_ProjectBase form_ProjectBase = new Form_ProjectBase();
            form_ProjectBase.SetWinType(0);
            form_ProjectBase.id = id.ToString();
            form_ProjectBase.Ini();
            form_ProjectBase.Text = this.Text;
            if (form_ProjectBase.ShowDialog() == DialogResult.OK)
            {
                string Sql = string.Format(@"Update vou_project
                    Set projectName='{1}'
                    ,projectAddress='{2}'
                    ,projectManager='{3}'
                    ,projectType='{4}'
                    ,remark='{5}'
                    Where id='{0}'"
                    , form_ProjectBase.projectMainModel.id
                    , form_ProjectBase.projectMainModel.projectName
                    , form_ProjectBase.projectMainModel.projectAddress
                    , form_ProjectBase.projectMainModel.projectManager
                    , form_ProjectBase.projectMainModel.projectType
                    , form_ProjectBase.projectMainModel.remark);
                index = class_MySqlDataBase.ExecuteSql(Sql);
            }
            form_ProjectBase.Dispose();
            return index;
        }
        public override void ViewData(object id)
        {
            MessageBox.Show(id.ToString());
        }
        private void DeleteTestSite(string MainId)
        {
            class_MySqlDataBase.ExecuteSql(string.Format(@"Delete From vou_project_test
                Where mainId='{0}'", MainId));
            //class_MySqlDataBase.ExecuteSql(string.Format(@"Delete From vou_sensorsite
            //    Where projectId='{0}'", MainId));
        }

        public override int DeleteData(object id)
        {
            if (class_MySqlDataBase.GetKeyCount("vou_sensorsite", "id", "projectId", id.ToString()) == 0)
            {
                string Sql = string.Format("delete from vou_project_test where Mainid = '{0}'", id.ToString());
                class_MySqlDataBase.ExecuteSql(Sql);
                Sql = string.Format("delete from vou_project_picture where Mainid = '{0}'", id.ToString());
                class_MySqlDataBase.ExecuteSql(Sql);
                Sql = string.Format("delete from vou_project where id = '{0}'", id.ToString());
                return class_MySqlDataBase.ExecuteSql(Sql);
            }
            else
            {
                MessageBox.Show("此项目信息已被使用，不允许删除!", "温馨提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return 0;
            }
        }

        public override void ToExcel(SaveFileDialog saveFileDialog)
        {
            const string FileName = "设备基本信息";
            string DataBaseFileName = string.Format("{0}{1}"
                , FileName
                , System.DateTime.Now.ToString("yyyyMMdd"));
            SaveFileDialog SaveFileDialog = new SaveFileDialog();
            SaveFileDialog.FileName = DataBaseFileName;
            SaveFileDialog.Filter = "EXCEL|*.xlsx";
            SaveFileDialog.Title = string.Format("导出{0}", FileName);
            if (SaveFileDialog.ShowDialog() == DialogResult.OK)
            {
                Class_ToExcel class_ToExcel = new Class_ToExcel();
                ArrayList SheetNameArray = new ArrayList();
                ArrayList SheetTitleArray = new ArrayList();
                ArrayList FieldTitleArray = new ArrayList();
                ArrayList AveFieldNameList = new ArrayList();
                bool IsTotal = false;
                bool IsAverage = false;
                bool IsHeader = false;
                string LeftFieldName = null;
                string ExcelFileName = DataBaseFileName;
                string SumFieldNameList = null;
                DataSet ExcelDataSet = new DataSet();

                #region 自定义
                SheetNameArray.Add(FileName);
                SheetTitleArray.Add(FileName);
                DataTable dataTable = new DataTable();
                dataTable = class_MySqlDataBase.GetEquipmentNoPage(1, 0);
                ExcelDataSet.Tables.Add(dataTable);
                #endregion

                class_ToExcel._GiveYouExcel(
                    SheetNameArray
                    , SheetTitleArray
                    , IsTotal
                    , ExcelFileName
                    , FieldTitleArray
                    , SumFieldNameList
                    , IsAverage
                    , AveFieldNameList
                    , IsHeader
                    , LeftFieldName
                    , ExcelDataSet);

                ExcelDataSet.Dispose();
                if (SheetNameArray.Count > 0)
                    SheetNameArray.Clear();
                if (SheetTitleArray.Count > 0)
                    SheetTitleArray.Clear();
                if (FieldTitleArray.Count > 0)
                    FieldTitleArray.Clear();
                if (AveFieldNameList.Count > 0)
                    AveFieldNameList.Clear();
            }

        }

        public override void ToWord(SaveFileDialog saveFileDialog)
        {
            base.ToWord(saveFileDialog);
        }

        public override void Finder(int PageNo)
        {
            waitDialogForm = new WaitDialogForm("正在玩命加载中......", "温馨提示");

            DataSet dataSet = this.GetFinder(PageNo, base.GetGridPage().PageSize, this.textEdit1.Text);
            base.SetGridDataSource(dataSet.Tables[0]);
            base.GetGridPage().SetRowCount(int.Parse(dataSet.Tables[1].Rows[0][0].ToString()));
            waitDialogForm.Close();
        }
        private DataSet GetFinder(int PageNo, int PageSize, string Address)
        {
            string SelectSql = "SELECT *";
            string FormSql = "FROM vou_project";
            string WhereSql = null;
            string GroupSql = null;
            string HavingSql = null;
            string OrderSql = "ORDER BY createDate";
            string TotalFieldName = "*";

            if (Address != null && Address.Trim().Length > 0)
            {
                WhereSql = string.Format("Where projectName like CONCAT('{0}','%') ", Address.Trim());
            }

            PageSqlModel pageSqlModel = class_MySqlDataBase.GetSql(SelectSql
            , FormSql
            , WhereSql
            , GroupSql
            , HavingSql
            , OrderSql
            , PageNo
            , PageSize
            , TotalFieldName);

            return class_MySqlDataBase.GetPageDataSet(pageSqlModel);
        }

        private void Form_Project_FormClosed(object sender, FormClosedEventArgs e)
        {
            if (waitDialogForm != null)
            {
                waitDialogForm.Dispose();
            }
            class_MySqlDataBase.CloseSqlConnection();
        }

        private void textEdit1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
            {
                Finder(base.GetGridPage().GetPageNo());
            }
        }
    }
}
